//
//  QRScan.swift
//  NativeQR
//
//  Created by 陈鑫 on 2020/5/28.
//  Copyright © 2020 Harvey. All rights reserved.
//

import UIKit

class QRScan: UIView {
    var delegate:QRScanDelegate?
    var line:UIView!
    // 有效扫码区域
    private let area = CGPoint(x: UIScreen.main.bounds.size.width/6, y: UIScreen.main.bounds.size.height/2-UIScreen.main.bounds.size.width*1/3-30)
    private let width = UIScreen.main.bounds.size.width * 2 / 3
    //动画宽度
    private let moveWidth:CGFloat = 4
    //动画间距
    private let moveMargin:CGFloat = 20
    //边框宽度
    private let borderWidth:CGFloat = 4
    //边框长度
    private let borderLength:CGFloat = 30
    //返回图标
    lazy var leftButton:UIButton = {
        let urlIcon = Bundle.main.url(forResource: "cancelScaner", withExtension: "png")
        let dataIcon = NSData(contentsOf: urlIcon!)
        let button:UIButton = UIButton.init()
        button.setImage(UIImage(data: dataIcon! as Data), for: .normal)
        //        /*向左进行偏移*/
        button.addTarget(self, action: #selector(cancel(_:)), for: .touchUpInside)
        return button
    }()
    init() {
        super.init(frame: UIScreen.main.bounds)
        let color:UIColor = .white
        self.status()
        self.drawDashLine(strokeColor: color, lineWidth: borderWidth)
        DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
            self.line = UIView(frame: CGRect(x: self.area.x+self.moveMargin, y: self.area.y, width: self.width - self.moveMargin, height: self.moveWidth))
            self.line.backgroundColor = UIColor.green
            self.addSubview(self.line)
            self.move()
        }
    }

    func status(){
        let screen = UIScreen.main.bounds
        let navigationBarHeight: CGFloat = 44
        let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: screen.size.width, height: navigationBarHeight))
        let leftLabel = UIBarButtonItem.init(customView: self.leftButton)
        let navigationItem = UINavigationItem()
        navigationItem.leftBarButtonItem = leftLabel
        navigationItem.title = "扫一扫"
        //设置title颜色
        navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
        //设置图标颜色
        navigationBar.tintColor = UIColor.white
        navigationBar.items = [navigationItem]
        //设置导航栏为沉浸式
        navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationBar.shadowImage = UIImage()
        self.addSubview(navigationBar)
    }
    
    @objc func cancel(_:Any){
        delegate?.cancel()
    }
    func move(){
        let path =  CGMutablePath()
        path.move(to: CGPoint(x:area.x + width / 2,y:area.y), transform: transform)
        path.addLine(to: CGPoint(x:area.x + width / 2 ,y:area.y + width), transform: transform)
        let orbit = CAKeyframeAnimation(keyPath:"position")
        orbit.duration = 2.5
        orbit.path = path
        // 转后台运行后动画不停止
        orbit.isRemovedOnCompletion = false
        // 动画循环
        orbit.repeatCount = HUGE
        line.layer.add(orbit, forKey:"Move")
    }
    func drawDashLine(strokeColor: UIColor, lineWidth: CGFloat) {
        let shapeLayer = CAShapeLayer()
        shapeLayer.anchorPoint = area
        shapeLayer.fillColor = UIColor.blue.cgColor
        shapeLayer.strokeColor = strokeColor.cgColor
        shapeLayer.lineWidth = lineWidth
        
        let path = CGMutablePath()
        path.move(to: CGPoint(x: area.x, y: area.y-borderWidth/2))
        path.addLine(to: CGPoint(x: area.x, y: area.y+borderLength))
        path.move(to: CGPoint(x: area.x, y: area.y+width-borderLength))
        path.addLine(to: CGPoint(x: area.x, y: area.y+width+borderWidth/2))

        path.move(to: area)
        path.addLine(to: CGPoint(x: area.x+borderLength, y: area.y))
        path.move(to: CGPoint(x: area.x+width-borderLength, y: area.y))
        path.addLine(to: CGPoint(x: area.x+width, y: area.y))

        path.move(to: CGPoint(x: area.x+width, y: area.y-borderWidth/2))
        path.addLine(to: CGPoint(x: area.x+width, y: area.y+borderLength))
        path.move(to: CGPoint(x: area.x+width, y: area.y+width-borderLength))
        path.addLine(to: CGPoint(x: area.x+width, y: area.y+width+borderWidth/2))

        path.move(to: CGPoint(x: area.x, y: area.y+width))
        path.addLine(to: CGPoint(x: area.x+borderLength, y: area.y+width))
        path.move(to: CGPoint(x: area.x+width-borderLength, y: area.y+width))
        path.addLine(to: CGPoint(x: area.x+width, y: area.y+width))
        
        shapeLayer.path = path
        self.layer.addSublayer(shapeLayer)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
